package org.loda.util;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 
 * @ClassName: ConfigReader
 * @Description: 读取配置文件中所有的过滤器
 * @author minjun
 * 
 */
public class ConfigReader {

	public static Document buildDoc(String path) {
		SAXReader reader = new SAXReader();
		try {
			InputStream is=ConfigReader.class.getResourceAsStream("/"+ (path == null ? "filter.xml" : path));
			return reader.read(is);
		} catch (DocumentException e) {
			throw new RuntimeException("加载xml文档错误，检查文档路径是否正确或者文档文件本身是否合法", e);
		}
	}

	public static List<String> read(String path) {
		Document doc = buildDoc(path);
		Element root = doc.getRootElement();
		Element filters = root.element("filters");

		List<String> list = new ArrayList<String>();

		for (Object element : filters.elements("filter")) {

			Element filter = (Element) element;
			list.add(filter.getTextTrim());
		}
		return list;
	}

}
